package com.jsle.common;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.jsle.common.dao.mybatis.BaseDao;
import com.jsle.financial.manager.UserManager;
import com.jsle.financial.util.Constants;
import com.jsle.financial.vo.Result;

/**
 * Manager 用于事物控制
 * 
 * @author zhangyanqing
 * @date 2012-10-27
 */
public class Manager {
    private Log log = LogFactory.getLog(UserManager.class);

    @Resource
    private BaseDao baseDao;

    /**
     * 新增一用户
     * 
     * @param user
     * @return
     */
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public <T> Result save(T object) {
	Result result = new Result();
	try {
	    result = baseDao.save(object);
	} catch (Exception e) {
	    String className = object.getClass().getSimpleName();
	    result.setResultCode(Constants.Manager.FC_RESULT_INSERT_TRANSACTIONAL_ERROR_CODE);
	    result.setResultMessage(String
		    .format(Constants.Manager.FC_RESULT_INSERT_TRANSACTIONAL_ERROR_MESSAGE,
		            className));
	    log.error("new " + className + " error## " + e.getMessage());
	}
	return result;
    }

}
